package Shared;

import java.io.Serializable;
import java.io.StringReader;
import java.io.StringWriter;
import java.util.ArrayList;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBElement;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.namespace.QName;

@XmlRootElement(name = "Players")
public class Players implements Serializable {
    private static final long serialVersionUID = 25L;
    
    public Players() {}
    
    @XmlElement
    public ArrayList<Player> liste = new ArrayList<Player>();

    // +----------------------------------------------------------------------+
    // | Serializer
    // +----------------------------------------------------------------------+
    public static String serialize(Players players) {
	try {
	    StringWriter writer = new StringWriter();
	    JAXBContext context = JAXBContext.newInstance(Players.class);
	    Marshaller m = context.createMarshaller();
	    m.marshal(new JAXBElement(new QName(Players.class.getSimpleName()),
		    Players.class, players), writer);
	    return writer.toString();
	} catch (Exception e) {
	    e.printStackTrace();
	}
	return "";
    }

    public static Players deserialize(String input) {
	try {
	    JAXBContext context = JAXBContext.newInstance(Players.class);
	    Unmarshaller m = context.createUnmarshaller();
	    return (Players) m.unmarshal(new StringReader(input));
	} catch (Exception e) {
	    e.printStackTrace();
	}
	return null;
    }
}